package android.gpstrac.data;

import java.io.*;

import android.content.ContentValues;
import android.gpstrac.controls.providers.GPSTracContentProvider;
import android.net.Uri;
import android.provider.BaseColumns;

/**
 * Each of these represents a user in the database system.
 *
 * @author Nicolas Papin
 */
public class User implements Transmittable, DatabaseStorable
{
	//
	// CLASS AND INSTANCE DATA
	//
	/**
	 * The username of the user.
	 */
	private String mName;
	
	/**
	 * The length of a username.
	 */
	final static public int USER_ID_LEN = 20;
	
	//
	// CTOR
	//
	public User(String name)
	{
		mName = name;
	}
	
	public User()
	{
		this(null);
	}
	
	//
	// METHODS
	//
	/* (non-Javadoc)
	 * @see android.gpstrac.data.Transmittable#getServerRepresentation()
	 */
	@Override
	public byte[] getServerRepresentation()
	{
		String name = (mName.trim().split(" "))[0];
		byte[] data = new byte[USER_ID_LEN], tmp;
		try
		{
			tmp = name.getBytes("US-ASCII");
			
			for(int i = 0; i < tmp.length; i++)
			{
				if(i >= (USER_ID_LEN - 1))
				{
					data[i]='\0';
					break;
				}
				else
					data[i] = tmp[i];
			}
		}
		catch (UnsupportedEncodingException ex)
		{
			data = null;
			ex.printStackTrace();
		}
		
		return data;
	}
	
	/* (non-Javadoc)
	 * @see android.gpstrac.data.Transmittable#fillWithTransmittedData(java.lang.Byte[])
	 */
	@Override
	public void fillWithTransmittedData(byte[] data)
	{
		if(data.length != USER_ID_LEN)
			return;
		
		InputStreamReader isr = null;
		String finalStr = "";
		
		try
		{
			isr = new InputStreamReader(new ByteArrayInputStream(data), "US-ASCII");
			
			int val = isr.read();
			char c;
			
			while(val != -1)
			{
				c = (char)val;
				if(!Character.isIdentifierIgnorable(c))
					finalStr += Character.toString(c);
				else
					break;
				
				val = isr.read();
			}
		}
		catch (UnsupportedEncodingException ex1)
		{
			ex1.printStackTrace();
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
		}
		
		mName = finalStr;
	}
	
	public String getName()
	{
		return mName;
	}

	/* (non-Javadoc)
	 * @see android.gpstrac.data.DatabaseStorable#getValueHolder()
	 */
	@Override
	public ContentValues getValueHolder()
	{
		ContentValues values = new ContentValues();
		values.put(UserContent.NAME, mName);
		
		return values;
	}

	//
	// INNER CLASS
	//
	public static final class UserContent implements BaseColumns
	{
		//
		// DATA
		//
		/**
		 * Table name for user content.
		 */
		public static final String TABLE_NAME = "friends";
		
		/**
		 * Index names for user content.
		 */
		public static final String USER_ID = "_id";
		public static final String NAME = "name";
		
		/**
		 * The content type.
		 */
		public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.gpstrac.user";
		
		/**
		 * Content URI
		 */
		public static final Uri CONTENT_URI = Uri.parse("content://" + GPSTracContentProvider.AUTHORITY + "/friends");
	}
}
